<template>
  <div class="goods-type-list">
    <div class="search-bar">
      <div class="search">
        <img src="../../../static/search.svg" alt="">
        <form @submit.prevent="">
          <input type="search" @keypress.enter="_getList" v-model="search">
        </form>
      </div>
      <div class="changeList">
        <van-icon name="apps-o" class="icon" @click="changeList = !changeList" size="0.5rem" />
      </div>
    </div>
    <div class="pick-bar">
      <div class="item" :class="{current: sort === ''}" @click="sortClick('')">综合</div>
      <div class="item" :class="{current: sort === 'total'}" @click="sortClick('total')">销量</div>
      <div class="item" :class="{current: sort === 'price'}" @click="sortClick('price')">
        价格
        <span>{{ isDesc }}</span>
      </div>
      <div class="item" :class="{current: goodsTypeCurrent.length > 0}" @click="typePick = true">筛选</div>
    </div>
    <transition name="slide">
      <div class="type-wrapper" v-if="typePick">
        <ul>
          <li
            v-for="item in goodsTypeList"
            @click="goodsTypeClick(item.class_name)"
            :class="{current: goodsTypeCurrent === item.class_name}"
            :key="item.id"
          >
            {{ item.class_name }}
          </li>
        </ul>
        <div>
          <span @click="cancle">取消筛选</span>
          <span @click="goodsTypeConfirm">确认</span>
        </div>
      </div>
    </transition>
    <Fade>
      <div class="mask" @click="typePick = false" v-show="typePick"></div>
    </Fade>
    <div class="content" :class="{'change': changeList === true}">
      <router-link :to="'goodsTypeList/goodsDetail/' + item.id" tag="div" class="item" v-for="item in list" :key="item.id">
        <div class="img">
          <img :src="item.url">
        </div>
        <h4>{{ item.title }}</h4>
        <h5>￥{{ item.price || item.m_price }}</h5>
        <div class="buy" @click.stop="buy(item.id)">购买</div>
      </router-link>
    </div>
    <Fade>
      <GoodsDialog v-if="dialog" @close="dialogClose" :data="detail"></GoodsDialog>
    </Fade>
  </div>
</template>

<script>
  import GoodsDialog from '@/components/goodsDialog/GoodsDialog.vue'
  import Fade from '@/components/fade/Fade.vue'
  import { mapState } from 'vuex'
  export default {
    name: 'GoodsTypeList',
    components: {
      GoodsDialog,
      Fade
    },
    created() {
      this._getList()
    },
    data() {
      return {
        dialog: false,
        typePick: false,
        list: [],
        detail: {},
        listType: '1',
        sort: '',
        search: '',
        priceSortCount: 0,
        goodsTypeList: [],
        goodsTypeCurrent: '',
        changeList: false,
        flag: true
      }
    },
    computed: {
      ...mapState(['openId', 'userState']),
      params() {
        let obj = {
          size: 1000,
          current: 1,
          openid: this.openId,
          class_name: this.goodsTypeCurrent,
          title: this.search
        }
        if(this.sort == '') {
          obj.sort = '-rise'
        }
        if(this.sort == 'total') {
          obj.sort = '-total'
        }
        if(this.sort == 'price') {
          let name = ""
          if(this.userState == 1) {
            name = 'price'
          } else {
            name = 'm_price'
          }
          if(this.isDesc === '(降)') {
            obj.sort = '-' + name
          } else {
            obj.sort = name
          }
        }
        return obj
      },
      isDesc() {
        if(this.priceSortCount == 0) {
          return ''
        }
        return this.priceSortCount % 2 == 0 ? '(降)' : '(升)'
      }
    },
    methods: {
      buy(id) {
        this._get(id)
      },
      dialogClose() {
        this.dialog = false
      },
      cancle() {
        this.goodsTypeCurrent = ''
        this._getList()
        this.typePick = false
      },
      goodsTypeClick(item) {
        // if(this.goodsTypeCurrent.includes(item)) {
        //   this.goodsTypeCurrent = this.goodsTypeCurrent.filter(it => {
        //     return item !== it
        //   })
        //   return
        // }
        // this.goodsTypeCurrent.push(item)
        if(this.goodsTypeCurrent == item) {
          this.goodsTypeCurrent = ''
          return
        }
        this.goodsTypeCurrent = item
      },
      goodsTypeConfirm() {
        this._getList()
        this.typePick = false
      },
      sortClick(sort) {
        this.sort = sort
        if(sort == 'price') {
          this.priceSortCount++
        } else {
          this.priceSortCount = 0
        }
        this._getList()
      },
      async _getList() {
        let { data: { code, data } } = await this.$axios({
          url: '/v1/goods/show',
          method: 'GET',
          params: this.params
        })
        if(code == 0) {
          this.list = data.items
          if(this.flag) {
            this.goodsTypeList = this.list
            this.flag = false
          }
        }
      },
      async _get(id) {
        let { data: { code, data } } = await this.$axios({
          url: '/v1/goods/list',
          method: 'GET',
          params: {
            id: id,
            openid: this.openId
          }
        })
        if(code == 0) {
          let guageList = data.gui.map(item => {
            item[0].count = 1
            item[0].price = item[0].price || item[0].m_price
            return item[0]
          })
          this.detail = {
            ...data.items[0],
            guageList
          }
          this.dialog = true
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import '@/assets/css/base.scss';
  .slide-enter, .slide-leave-to {
    transform: translate3d(0, -100%, 0);
  }
  .slide-enter-active, .slide-leave-active {
    transition: all 0.1s linear;
  }
  .goods-type-list {
    background: #eee;
    padding-top: 1.5rem;
    min-height: 100vh;
  }
  .search-bar {
    position: fixed;
    display: flex;
    top: 0;
    width: 100%;
    padding-left: 0.2rem;
    align-items: center;
    height: 0.8rem;
    border-bottom: 1px solid $bgGray;
    background: #fff;
    z-index: 10;
    .search {
      flex: 1;
      display: flex;
      align-items: center;
      padding-right: 0.2rem;
      background: $bgGray;
      height: 0.6rem;
      border-radius: 5px;
      img {
        flex: 0 0 0.6rem;
        width: 0.6rem;
      }
      form {
        flex: 1;
        height: 0.6rem;
        input {
          width: 100%;
          background: $bgGray;
          height: 0.6rem;
          font-size: 0.28rem;
        }
      }
    }
    .changeList {
      flex: 0 0 1rem;
      width: 1rem;
      height: 0.8rem;
      line-height: 0.8rem;
      text-align: center;
      .icon{
        margin-top: 0.15rem;
      }
    }
  }
  .pick-bar {
    position: fixed;
    display: flex;
    width:100%;
    padding: 0.2rem 0;
    top: 0.8rem;
    font-size: 0.28rem;
    border-bottom: 1px solid $bgGray;
    background: #fff;
    z-index: 10;
    div {
      flex: 1;
      text-align: center;
      border-right: 1px solid $bgGray;
    }
    .current {
      color: $colorRed;
    }
  }
  .type-wrapper {
    position: fixed;
    background: #fff;
    top: 1.6rem;
    left: 0;
    width: 100%;
    box-shadow: 0px 2px 5px 0px $colorGray;
    padding: 0 0.2rem;
    z-index: 9;
    li {
      display: inline-block;
      margin: 0.1rem;
      border: 1px solid $colorGray;
      padding: 0.14rem 0.14rem;
      font-size: 0.24rem;
      border-radius: 5px;
      &.current {
        border: 1px solid $colorRed;
        color: $colorRed;
      }
    }
    div {
      position: relative;
      padding: 0.1rem 0;
      border-top: 1px solid $bgGray;
      font-size: 0.28rem;
      span:nth-child(2) {
        position: absolute;
        right: 0.2rem;
        color: $colorRed;
      }
    }
  }
  .mask {
    position: fixed;
    top: 0.15rem;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 8;
  }
  .content {
    position: relative;
    // top: 1.5rem;
    padding: 0.1rem 0rem 55px 0rem;
    overflow: hidden;
    background: #eee;
    
    .item {
      display: inline-block;
      position: relative;
      margin: 0.1rem;
      // padding: 0.1rem;
      width: 47%;
      background: #fff;
      .img {
        position: relative;
        width: 100%;
        padding: 50%;
        img {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
        }
      }
      h4 {
        margin-top: 0.2rem;
        font-size: 0.28rem;
        padding: 0 0.1rem;
        
      }
      h5 {
        margin-top: 0.4rem;
        margin-bottom: 0.1rem;
        color: $colorRed;
        font-size: 0.28rem;
        margin-left: 0.1rem;
      }
      .buy {
        position: absolute;
        padding: 0.05rem 0.5rem;
        background: $colorRed;
        color: #fff;
        right: 0.1rem;
        bottom: 0.1rem;
      }
    }
  }
  .content.change {
    padding-bottom: 0.5rem;
    .item {
      display: flex;
      width: 100%;
      margin: 0;
      border-bottom: 1px solid $bgGray;
      .img {
        flex: 0 0 1.5rem;
        width: 1.5rem;
        height: 1.5rem;
        margin-right: 0.2rem;
        padding: 0;
        img {
          position: static;
        }
      }
      h5 {
        position: absolute;
        left: 1.74rem;
        bottom: 0.08rem;
      }
    }
  }
</style>
